<!DOCTYPE html>
<html>
<head>
	<title>二叉树-层序遍历</title>
</head>
<body>
<script type="text/javascript">
const treeRoot = {
	val: 3,
	left: { 
		val: 9,
		left: { 
			val: 22,
			left: {
				val: 26
			}, 
			right: null 
		},
		right: { val: 12, left: null, right: null },
	},
	right: {
		val: 20,
		left: { val: 15, left: null, right: null },
		right: { val: 7, left: null, right: null },
	},
};
</script>
<script type="text/javascript">
const levelOrder1 = function(root) {
	let result = []
    let stack = []

    stack.push(root);//进入队列
    // 先遍历第一层,然后第二层,第三层...
    while(stack.length > 0){
        let cutNode = stack.shift();//出队

        result.push(cutNode.val);

        if(cutNode.left){
            stack.push(cutNode.left);//入队
        }
        if(cutNode.right){
            stack.push(cutNode.right);//入队
        }
        
    }

    return result;
};
console.log(treeRoot);
console.log('层次遍历:', levelOrder1(treeRoot))
</script>
</body>
</html>